%7.2
    %a
x = 1:0.01:2;
y = x.^2 + 4*cos(x);
plot(x,y)
    %b
a = 1; b = 2;j = 0;
eps = 0.2;
f = @(x) x^2 + 4 * cos(x);
while(b - a > eps)
    a1 = a + 0.382 * (b - a);
    b1 = a + 0.618 * (b - a);
    if(f(a1)>f(b1))
        a = a1;
    else
        b = b1;
    end
    j = j + 1;              %确定黄金分割法的迭代次数，为%d题做铺垫
end
disp([a b])
    %c
a = 1;b = 2;
e = 0.05;
esp = 0.2;
c =(1+2*e)/(esp/(b-a));
f = @(x) x^2 + 4 * cos(x);
s=[1 1 2 3 5 8 13 21]; i=5;
while(i>=1)
   
    p(i) = 1 - s(i)/s(i+1);
    a1 = a + p(i)*(b - a);
    b1 = a + (1 - p(i))*(b - a);
    if (f(a1)>f(b1))
        a = a1;
    else
        b = b1;
    end
    i = i - 1;
end
disp([a b])
 
 
    %d
syms x;
y = x.^2 + 4 * cos(x);
y1 = diff(y,x,1);
y2 = diff(y,x,2);
z1 = matlabFunction(y1);
z2 = matlabFunction(y2);
x0 = 1;
for k=1:1:4
    x1 = x0 - (z1(x0)/z2(x0));
    x2 = x0;
    x0 = x1;
    
end
%7.3
%a
syms x;
fplot(8*exp(1-x)+7*log(x))          %画出全部的的函数图像，需要安装symbolic math tool
%b
a =1;b = 2;
eps = 0.23;
f = @(x)8*exp(1-x)+7*log(x);
while(b - a >0.23)
    a1 = a + 0.382 * (b - a);
    b1 = a + 0.618 * (b - a);
    if(f(a1)>f(b1))
        a = a1;
    else
        b = b1;
    end
end
disp([a b])
%c
a =1;b = 2;
eps = 0.23;
e = 0.05;
c = (1 + 2 * e) / 0.23
f = @(x)8*exp(1-x)+7*log(x);
s = [1 1 2 3 5];
k=1;
for i = 5:-1:2
    p(k) = 1 - s(i-1)/s(i)
    a1 = a + p(k) * (b-a);
    b1 = a +(1- p(k)) * (b - a);
    if(f(a1)>f(b1))
        a = a1;
    else
        b = b1;
    end
    k = k  + 1;
end
%16.20
c = [2 -1 -1 0];
a = [3 1 0 1;
    6 2 1 1];
b = [4;5];
v = 1:1:4;
d = [a,b];
d(3,:)=[c,0];           %d是单纯形表
d(2,:)=d(2,:)-d(1,:)
d





%网上。需要改换矩阵
A=[3 1 0 1;
    6 2 1 1];
c=[2 -1 -1 0];
b=[4 5]';
%A为系数矩阵
%b为常数约束矩阵
%c为目标函数系数矩阵
for i=1:length(c)
    if c(i)<0    %选择底行c从左往右数第一个负数
        xishu=zeros(length(A(:,1)),1)+10000; %初始化为大数以避免影响最小值
        for j=1:length(A(:,1))
          if A(j,i)>0
              xishu(j,1)=b(j,1)/A(j,i);  %把整数行的比值存起来
          end
        end
        [R,C]=find(xishu==min(xishu));   %找到比值最小的行数R
        h=1/A(R,i);
        A(R,:)= A(R,:).*h; %把A对应行的化为1
        b(R)=b(R)*h;    %把b对应行做变换
        for z=1:length(b)-1   %把A中其他行对应的化为0
            if z~=R
                h=A(z,i);
                A(z,:)=A(z,:)-A(R,:)*h;
                b(z)=b(z)-b(R)*h;
            end
        end
        h=c(1,i);
        c(1,:)=c(1,:)-h*A(R,:);  %处理c行化为0
        b(length(b))=b(length(b))-h*b(R);%处理c行对应的b
    end
end
disp("最优解为：")  %从A和b中看出最优解
disp([4,3]')
disp("最优值为：")
disp(-b(length(b)))   %从c中得最优值
